package com.atguigu.guli.service.cms.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.cms.entity.AdType;
import com.atguigu.guli.service.cms.service.AdTypeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 推荐位 前端控制器
 * </p>
 *
 * @author Miluo
 * @since 2020-10-31
 */
@RestController

@Api(tags = "广告分类管理")
@RequestMapping("/admin/cms/ad-type")
public class AdminAdTypeController {
    @Autowired(required = true)
    private AdTypeService adTypeService;


    @ApiOperation("所有广告类型列表")
    @GetMapping("/list")
    public R adTypeList(){
        List<AdType> list = adTypeService.list();
        return R.ok().data("items",list);
    }

    @ApiOperation("广告列表分页")
    @GetMapping("/list/{page}/{limit}")
    public R adTypePage(@PathVariable Long page,@PathVariable Long limit){
        Page<AdType> pages = new Page<AdType>(page,limit);
        Page<AdType> adTypePage = adTypeService.page(pages);
        List<AdType> records = adTypePage.getRecords();
        long total = adTypePage.getTotal();
        return R.ok().data("total",total).data("rows",records);

    }


    @ApiOperation("删除广告类型")
    @DeleteMapping("remove/{id}")
    public R removeAdTyppe(@PathVariable String id){
        boolean b = adTypeService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("无此数据");
        }
    }


    @ApiOperation("新增广告类型")
    @PostMapping("/save")
    public R addAdType(@RequestBody AdType adType){
        boolean save = adTypeService.save(adType);
        if (save){
            return R.ok().message("添加成功");
        }else {
            return R.error().message("添加失败");
        }
    }


    @ApiOperation("修改广告类型")
    @PutMapping("/update")
    public R updateAdType(@RequestBody AdType adType){
        boolean b = adTypeService.updateById(adType);
        if (b){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }
    }

    @GetMapping("get/{id}")
    @ApiOperation("根据Id获取广告信息")
    public R getAdTypeById(@PathVariable String id){
        AdType adType = adTypeService.getById(id);
        return R.ok().data("item",adType);
    }

}

